
//
//  CYBettingNumber.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/7/20.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYBettingNumber.h"
#import "CYBettingRandomNumber.h"
#define A1(n) [self getArrangement:n];   //n的阶乘
#define A(n,m) [self getArrangement:m from:n]//从n个元素里面，取m个元素的排列数
#define C(n,m) [self getCombination:m from:n]//从n个元素里面，取m个元素的组合数
#define C_N_1(Arr,count) [self getNumberFrom:Arr arrCount:count]//从n个数组里面，每个数组取1个元素的组合数
#define C_1_N(Arr,n) [self getCombinationFrom:Arr maxCount:n]//从1个数组里，取n个元素的组合数
#define A_1_N(Arr,n) [self getArrangementFrom:Arr maxCount:n]//从1个数组里，取n个元素的排列数
#define C_RX(numCount,arrCount,Arr) [self renxuanCombination:numCount arrayCount:arrCount array:Arr]//任选，例子:五星-二星组选-任二组选复式,numCount为任选的最低要求号码的个数,arrayCount为最低要求数组的个数
#define A_RX(numCount,arrCount,Arr) [self renxuanArrangement:numCount arrayCount:arrCount array:Arr]//任选，例子:五星-二星组选-任二组选复式,numCount为任选的最低要求号码的个数,arrayCount为最低要求数组的个数
#define DiPinCaiType 16
#define ShiYiXuanWuType 77
#define KuaiSanType 39
#define ShiShiLeType 101
#define ShiShiCaiType 2
#define PKShiType 27
#define C_2(Arr,Str,multipe,sigle)  [self getCombinationFrom:Arr WithEqualString:Str multipeChoice:multipe sigleChoice:sigle]//从两个数组中，一个取n个元素(需要多个元素组成投注)，一个取m个元素(只需1个元素即可组成投注)，的组合数
@implementation CYBettingNumber
//根据选中号码的位级(万位，千位，十位，个位)/(第一位，第二位，第三位),和集合，返回 选中号码的数组(筛选出重复集合，空集合)
+ (NSMutableArray *)getSelectArrFrom:(NSMutableArray *)array title:(NSString *)title set:(NSMutableSet*)set{
    NSMutableArray * newArr = [array mutableCopy];
    BOOL ishave = NO;
    if (newArr.count > 0 ) {
        for (int i = 0; i < newArr.count; i ++) {
            NSMutableDictionary *dict = newArr[i];
            NSString *key = [[dict allKeys] firstObject];
            if ([title isEqualToString:key]){//如果相同
                ishave = YES;
                NSMutableDictionary *newDict = [NSMutableDictionary dictionary];
                [newDict setValue:set forKey:title];//替换集合
                [newArr replaceObjectAtIndex:i withObject:newDict];
            }
        }
    }
    if (!ishave) {//如果没有相同的key
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setValue:set forKey:title];
        [newArr addObject:dict];//增加新集合
    }
    //删除空集合
    for (NSMutableDictionary *dict in newArr.reverseObjectEnumerator) {
        NSSet *set = [[dict allValues] firstObject];
        if (!set)
            [newArr removeObject:dict];
        else if (set.count < 1)//
            [newArr removeObject:dict];
    }
    return newArr;
}

//------------------------------------------------随机算法---------------------------------------------------------------------------------------------------------------------------------------------------------//
//机选号码
+ (NSMutableArray *)getRandomSource:(NSUInteger)played_group_id playedIdentifier:(NSString *)identifier cellArr:(NSArray *)cellArr {
    return [CYBettingRandomNumber getRandomSource:played_group_id playedIdentifier:(NSString *)identifier cellArr:(NSMutableArray *)cellArr];
}

//--------------------------------------------------------投注算法------------------------------------------------------------------------------------------------------------------------------------------------//
//根据彩种类型，一级玩法，二级玩法，选中号码的数组，算出投注数目
+ (NSUInteger)getNumberForBetting:(NSUInteger)played_group_id playedIdentifier:(NSString *)identifier selectData:(NSArray *)array{
    if (array.count < 1)
        return 0;
    if ([@[@"ssc"] containsObject:identifier])
        return C_N_1(array, 5);
    else if ([@[@"ssc_dx_q4f",@"ssc_dx_h4f"]containsObject:identifier])
        return C_N_1(array, 4);
    else if ([@[@"ssc_3x_q3f",@"ssc_3x_z3fs",@"ssc_3x_h3f",@"ssc_dxds_q3",@"ssc_dxds_h3",@"ssl_3x_3zxf"]containsObject:identifier])
        return C_N_1(array, 3);
    else if ([@[@"ssc_2x_q2f",@"ssc_2x_h2f",@"ssc_dxds_q2",@"ssc_dxds_h2",@"three_2x_q2f",@"ssl_2x_q2zxf",@"ssl_2x_h2zxf",@"ssl_dxds_q2dxds",@"ssl_dxds_h2dxds"]containsObject:identifier])
        return C_N_1(array, 2);
    else if ([@[@"s115_rx8"] containsObject:identifier])
        return C_1_N(array, 8);
    else if ([@[@"s115_rx7"] containsObject:identifier])
        return C_1_N(array, 7);
    else if ([@[@"s115_rx6"] containsObject:identifier])
        return C_1_N(array, 6);
    else if ([@[@"ssc_5x_z5_120",@"s115_rx5"] containsObject:identifier])
        return C_1_N(array, 5);
    else if ([@[@"ssc_4x_z4_24",@"s115_rx4"] containsObject:identifier])
        return C_1_N(array, 4);
    else if ([@[@"ssc_z3_q6",@"ssc_z3_z6",@"ssc_5x_3m_bdw",@"s115_q3_zu",@"s115_rx3",@"k3_3x"] containsObject:identifier])
        return C_1_N(array, 3);
    else if ([@[@"ssc_4x_z4_6",@"ssc_z2_f2",@"ssc_2x_qbdw",@"ssc_2x_hbdw",@"ssc_5x_2m_bdw",@"ssc_4x_2m_bdw",@"s115_rx2",@"ssc_2x_bdw",@"k3_2x",@"ssl_2x_h2zf",@"ssl_bdw_2mbdw",@"s115_q2_zu",@"ssl_2x_q2zf"] containsObject:identifier])
        return C_1_N(array, 2);
    else if ([@[@"ssc_ts_h3",@"ssc_hz_h3",@"ssc_5x_q3",@"ssc_5x_z3",@"ssc_5x_h3",@"ssc_4x_1m_bdw",@"qwwf_yffs",@"s11x5_q1z",@"ssc_11x5_bdw",@"s115_rx1",@"qwwf_czw",@"pk10_lm_dxds1",@"pk10_lh",@"pk10_x1",@"k3_hz",@"k3_3dx",@"k3_3tx",@"k3_3ltx",@"k3_2fx",@"ssl_bdw_1mbdw"] containsObject:identifier])
        return C_1_N(array, 1);
    else if ([@[@"ssc_5x_z5_60"] containsObject:identifier])
        return C_2(array, @"单号", 1, 3);//从两个数组中，一个取n个元素(需要多个元素组成投注)，一个取m个元素(只需1个元素即可组成投注)，的组合数
    else if ([@[@"ssc_4x_z4_4",@"ssc_5x_z5_10",@"ssc_5x_z5_5",@"bjpk10_x2",@"s115_q2_zhi"] containsObject:identifier])
        return [self getNumberWithTwo:array];
    else if ([@[@"ssc_5x_z5_20",@"ssc_4x_z4_12"] containsObject:identifier])
        return C_2(array, @"单号", 1, 2);
    else if ([@[@"ssc_5x_z5_30"] containsObject:identifier])
        return C_2(array, @"二重号", 1, 2);
    //    else if ([@[@"ssc_5x_z5_10"] containsObject:identifier])
    //        return [self getNumberWithTwo:array];
    //    else if ([@[@"ssc_5x_z5_5"] containsObject:identifier])
    //        return [self getNumberWithTwo:array];
    //    else if ([@[@"bjpk10_x2"] containsObject:identifier])
    //        return [self getNumberWithTwo:array];
    else if ([@[@"k3_2dx"] containsObject:identifier]){
        if (array.count < 2)
            return 0;
        else
            return C([[[[array[0] allValues] firstObject] allObjects] count],1) * C([[[[array[1] allValues] firstObject] allObjects] count],1);
    }
    else if ([@[@"ssc_z3_q3",@"ssc_z3_z3",@"ssl_3x_z3f"] containsObject:identifier])
        return A_1_N(array, 2);//从1个数组里，取n个元素的排列数
    else if ([@[@"s115_q3_zhi",@"bjpk10_x3"] containsObject:identifier])
        return [self getNumberWithThree:array];
    else if ([@[@"ssc_11x5_dwd",@"bjpk10_dw",@"three_3x_dw",@"ssl_dwd_dwd"] containsObject:identifier])
        return [self sum:array];
    else if ([@[@"ssc_q2_zhi_hz",@"ssc_h2_zhi_hz"] containsObject:identifier])
        return [self sumForTwo:array min:0 max:9 containDuiZiHao:YES];
    else if ([@[@"ssc_qh2_zhu_hz",@"ssc_h2_zhu_hz"] containsObject:identifier])
        return [self sumForTwo:array min:0 max:9 containDuiZiHao:NO];
    else if ([@[@"ssl_3x_zxhz",@"s3d_hz"] containsObject:identifier])//时时乐-三星-直选和值
        return [self sumForThree:array min:0 max:9 contain:1];
    else if ([@[@"ssl_3x_z3hz"] containsObject:identifier])//时时乐-三星-组三和值
        return [self sumForThree:array min:0 max:9 contain:2];
    else if ([@[@"ssl_3x_z6hz"] containsObject:identifier])//时时乐-三星-组六和值
        return [self sumForThree:array min:0 max:9 contain:3];
    else if ([@[@"ssc_5x_dw"] containsObject:identifier])
        return [self sum:array];
    //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
    else if ([@[@"ssc_z2_r2"] containsObject:identifier])//时时彩-二星组选-任二组选复式
        return C_RX(2, 2, array);
    else if ([@[@"ssc_5x_r3"] containsObject:identifier])//时时彩-不定胆-任三一码
        return C_RX(1, 3, array);
    else if ([@[@"ssc_z3_r3"] containsObject:identifier]){//时时彩-任选-任三组三
        if ([[[array[0] allValues] firstObject] count] < 2 || array.count < 3)
            return 0;
        return C([[[array[0] allValues] firstObject] count], 2) * C(array.count, 3) * 2;
    }
    else if ([@[@"ssc_z3_r6"] containsObject:identifier]){//时时彩-任选-任三组六
        if ([[[array[0] allValues] firstObject] count] < 3 || array.count < 3)
            return 0;
        return C([[[array[0] allValues] firstObject] count], 3) * C(array.count, 3);
    }
    else if ([@[@"ssc_2x_r2f",@"three_dxds_r2"] containsObject:identifier])//时时彩-任选-任二直选复式
        return [self shishicaiRenXuan:array count:2];
    else if ([@[@"ssc_3x_r3f"] containsObject:identifier])//时时彩-任选-任三直选复式
        return [self shishicaiRenXuan:array count:3];
    else if ([@[@"ssc_dx_r4f"] containsObject:identifier])//时时彩-任选-任四直选复式
        return [self shishicaiRenXuan:array count:4];
    else if ([@[@"ssc_h3_kd"] containsObject:identifier]){
        NSUInteger number = 0;
        for (NSDictionary *dict in array) {
            NSArray *subArr = [[[dict allValues] firstObject] allObjects];
            for (NSString *string in subArr) {
                if ([string intValue] == 0)
                    number += 10;
                else if ([string intValue] == 1 || [string intValue] == 9)
                    number += 54;
                else if ([string intValue] == 2 || [string intValue] == 8)
                    number += 96;
                else if ([string intValue] == 3 || [string intValue] == 7)
                    number += 126;
                else if ([string intValue] == 4 || [string intValue] == 6)
                    number += 144;
                else if ([string intValue] == 5)
                    number += 150;
            }
        }
        return number;
        
    }
    NSAssert(NO, @"时时彩彩票唯一标识符错误 %@",identifier);
    return 0;
}

//---------------------------------------------------------排列组合算法------------------------------------------------------------------------------------------------------------------------------------------------//
//计算组合算法,从n个元素取m个元素的组合
+ (NSUInteger)getCombination:(NSUInteger)M from:(NSUInteger)number{
    NSAssert(M <= number, @"M不能大于number/选取的元素不能大于元素集合的个数");
    if (M <= 0 || number <= 0)
        return 0;
    if (M == number)
        return 1;
    return  [self getArrangement:M from:number] / [self getArrangement:M];
    
}

//计算排列算法，从n个元素选m个元素的排列
+ (NSUInteger)getArrangement:(NSUInteger)M from:(NSUInteger)number{
    NSAssert(M <= number, @"M不能大于number/选取的元素不能大于元素集合的个数");
    if (number<=25)//因为26取1的排列数，已经超出long占有的字节，所以，对大于26的取1的排列算法，要加个参数，即总元素 - 取多少个元素 = 差值
        return [self getArrangement:number]/[self getArrangement:(number - M)];
    else
        return [self getArrangement:M from:number Dvalue:(number - M)];
}

//特殊排列算法（排列大于25的排列算法）
+ (NSUInteger)getArrangement:(NSUInteger)M from:(NSUInteger)number Dvalue:(NSUInteger)Dvalue{
    NSAssert(M <= number, @"M不能大于number/选取的元素不能大于元素集合的个数");
    if (M == number || number == 1 || number == Dvalue)
        return 1;
    return [self getArrangement:M from:(number - 1) Dvalue:Dvalue] * number;
}

//计算排列数算法，从n个元素选n个元素的排列
+ (NSUInteger)getArrangement:(NSUInteger)number{
    if (number == 0)
        return 1;
    else
        return [self getArrangement:number - 1] * number;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
//从n个数组，每个数组至少选取一个元素，的组合数 ,例子-->时时彩--五星复式
+ (NSUInteger)getNumberFrom:(NSArray *)array arrCount:(NSUInteger)count{
    if (array.count < count)
        return 0;
    return [self getNumberFrom:array index:0];
}

//从n个数组，每个数组至少选取一个元素，的组合数
+ (NSUInteger)getNumberFrom:(NSArray *)array index:(NSUInteger)index {
    if (array.count == index)
        return 1;
    else
        return [self getNumberFrom:array index:index + 1] * [[[array[index] allValues] firstObject] count];
}

//从1一个n个元素的数组里，取n个元素，的排列数,例子-->前三组三
+ (NSUInteger)getArrangementFrom:(NSArray *)array maxCount:(NSUInteger)maX{
    NSMutableArray *newArr = [[array[0] allValues] firstObject];
    return  (newArr.count >= maX)?A(newArr.count, maX):0;
}

//从1个n个元素的数组里，取n个元素，的组合数 ,例子-->时时彩--组选120
+ (NSUInteger)getCombinationFrom:(NSArray *)array maxCount:(NSUInteger)maX{
    
    NSMutableArray *newArr = [[array[0] allValues] firstObject];
    return  (newArr.count >= maX)?C(newArr.count, maX):0;
}
//从两个数组里，第1个数组取1个元素(二重号)，第2个数组取3个元素(单号)，的组合数  例子-->时时彩--组选60
/**
 *@pragma equal string 为单选号码
 *@pragma multipeNum 最低需要几个多重号，完成一次投注
 *@pragma sigleNum 最低需要几个单号组成一次投注
 */
+ (NSUInteger)getCombinationFrom:(NSArray *)array WithEqualString:(NSString *)string multipeChoice:(NSUInteger )multipeNum sigleChoice:(NSUInteger)sigleNum {
    //
    NSUInteger number = 0;
    NSString *key1 = [[array[0] allKeys] firstObject];
    NSString *key2 = (array.count >= 2)?[[array[1] allKeys] firstObject]:nil;
    id value1 = [[array[0]allValues] firstObject];
    id value2 = (array.count >= 2)?[[array[1]allValues] firstObject]:nil;
    NSMutableSet *newValue1 = ([value1 isKindOfClass:[NSMutableSet class]] || [value1 isKindOfClass:[NSSet class]])?[NSMutableSet setWithSet:value1]:[NSMutableSet setWithArray:value1];
    NSMutableSet *newValue2 = ([value2 isKindOfClass:[NSMutableSet class]] || [value1 isKindOfClass:[NSSet class]])?[NSMutableSet setWithSet:value2]:[NSMutableSet setWithArray:value2];
    
    if (([key1 isEqualToString:string]) && [value2 count] >= multipeNum && [value1 count] >= sigleNum){//如果key1是单号，则value2是二重号的集合
        [newValue2 intersectSet:newValue1];//取二重号与单号的交集
        number = C([value1 count], sigleNum) * [value2 count] - C(([value1 count] - 1), sigleNum - 1) * newValue2.count;//二重号与单号总的组合数 - 交集(代表相同的元素)组合数(因为交集的组合数，有1个元素，二重号单号都有，所以这里的组合数，位C(count - 1, sigleNum - 1))
    }
    else if (([key2 isEqualToString:string]) && [value1 count] >= multipeNum && [value2 count] >= sigleNum){//如果key2是单号，则value1是二重号的集合
        [newValue1 intersectSet:newValue2];
        number = C([value2 count], sigleNum) * [value1 count] - C(([value2 count] - 1), sigleNum - 1) * newValue1.count;
    }
    
    return number;
}

//任选，号码为组合方式 例子:五星-二星组选-任二组选复式,numCount为任选的最低号码的个数,arrayCount为最低数组的个数,
+ (NSUInteger)renxuanCombination:(NSUInteger)numCount arrayCount:(NSUInteger)arrayCount array:(NSArray *)array{
    if (array.count < arrayCount)
        return 0;
    else if ( [[[array[0] allValues] firstObject] count] < numCount)
        return 0;
    return C(array.count, arrayCount) * C([[[array[0] allValues] firstObject] count], numCount);
}

//任选，号码为排列方式 例子:五星-二星组选-任二组选复式,numCount为任选的最低号码的个数,arrayCount为最低数组的个数,
+ (NSUInteger)renxuanArrangement:(NSUInteger)numCount arrayCount:(NSUInteger)arrayCount array:(NSArray *)array{
    if (array.count < arrayCount)
        return 0;
    else if ( [[[array[0] allValues] firstObject] count] < numCount)
        return 0;
    return C(array.count, numCount) * A([[[array[0] allValues] firstObject] count], numCount);
}
//任选
+ (NSUInteger)getRenXuan:(NSArray *)array{
    NSUInteger startCount = 0;
    NSUInteger totalCount = 0;
    NSMutableArray *newArr = [NSMutableArray arrayWithArray:array];
    for (int i = 0; i < array.count; i ++) {
        startCount = [[[newArr[0] allValues] firstObject] count];
        for (int j = 0 ; j < newArr.count; j ++) {
            if (j + 1 < newArr.count)
                totalCount = (startCount * [[[newArr[j + 1] allValues] firstObject] count] + totalCount);
        }
        [newArr removeObjectAtIndex:0];
    }
    return totalCount;
}

//迭代 n个集合，任意两个集合中选中的元素不能重合，的组合数
+ (NSUInteger)getNumberWithThree:(NSArray *)array{
    if (array.count < 3)
        return 0;
    NSUInteger number = 0;
    NSMutableSet *value1 = [[array[0]allValues] firstObject];
    NSMutableSet *value2 = [[array[1]allValues] firstObject];
    NSMutableSet *value3 = [[array[2]allValues] firstObject];
    for (NSString *n1 in value1) {
        for (NSString *n2 in value2) {
            for (NSString *n3 in value3) {
                if (!([n1 intValue] == [n2 intValue]) && !([n1 intValue] == [n3 intValue]) &&  !([n2 intValue] ==[n3 intValue])){
                    number ++;
                }
            }
        }
    }
    return number;
}
//迭代 n个集合，任意三个个集合中选中的元素不能重合，的组合数
+ (NSUInteger)getNumberWithTwo:(NSArray *)array{
    if (array.count < 2)
        return 0;
    NSUInteger number = 0;
    NSMutableSet *value1 = [[array[0]allValues] firstObject];
    NSMutableSet *value2 = [[array[1]allValues] firstObject];
    for (NSString *n1 in value1) {
        for (NSString *n2 in value2) {
            if ([n1 intValue] != [n2 intValue]){
                number ++;
            }
        }
    }
    return number;
}

//n个集合，获取所有集合的元素个数，比如时时彩-定位胆-五星定位胆
+ (NSUInteger)sum:(NSArray *)array{
    NSUInteger number = 0;
    for (int i = 0 ; i < array.count; i ++) {
        NSArray *subArr = [[[array[i]allValues] firstObject] allObjects];
        number = subArr.count + number;
    }
    return number;
}

+ (NSUInteger)shishicaiRenXuan:(NSArray *)array count:(NSUInteger)count{
    if (array.count < count)
        return 0;
    else if (count == 2 )
        return [self two:array];
    else if (count == 3)
        return [self three:array];
    else if (count == 4)
        return [self four:array];
    else if (count == 5)
        return [self five:array];
    return 0;
}

+ (NSUInteger)two:(NSArray *)array{
    NSUInteger number = 0;
    for (int i = 0; i < array.count; i++) {
        for (int j = i + 1; j < array.count; j++) {
            number += [[[[array[i] allValues] firstObject] allObjects] count] * [[[[array[j] allValues] firstObject] allObjects] count];
        }
    }
    return number;
}

+ (NSUInteger)three:(NSArray *)array{
    NSUInteger number = 0;
    for (int i = 0; i < array.count; i++) {
        for (int j = i + 1; j < array.count; j++) {
            for (int m = j + 1; m < array.count; m++) {
                number += [[[[array[i] allValues] firstObject] allObjects] count] * [[[[array[j] allValues] firstObject] allObjects] count] * [[[[array[m] allValues] firstObject] allObjects] count];
            }
        }
    }
    return number;
}
+ (NSUInteger)four:(NSArray *)array{
    NSUInteger number = 0;
    for (int i = 0; i < array.count; i++) {
        for (int j = i + 1; j < array.count; j++) {
            for (int m = j + 1; m < array.count; m++) {
                for (int n = m + 1; n < array.count; n++) {
                    number += [[[[array[i] allValues] firstObject] allObjects] count] * [[[[array[j] allValues] firstObject] allObjects] count] * [[[[array[m] allValues] firstObject] allObjects] count] * [[[[array[n] allValues] firstObject] allObjects] count];
                }
            }
        }
    }
    return number;
}

+ (NSUInteger)five:(NSArray *)array{
    NSUInteger number = 0;
    for (int i = 0; i < array.count; i++) {
        for (int j = i + 1; j < array.count; j++) {
            for (int m = j + 1; m < array.count; m++) {
                for (int n = m + 1; n < array.count; n++) {
                    for (int h = n + 1; h < array.count; h++) {
                        number += [[[[array[i] allValues] firstObject] allObjects] count] * [[[[array[j] allValues] firstObject] allObjects] count] * [[[[array[m] allValues] firstObject] allObjects] count] * [[[[array[n] allValues] firstObject] allObjects] count] * [[[[array[h] allValues] firstObject] allObjects] count];
                    }
                }
            }
        }
    }
    return number;
}

//两位数的和值玩法,min->本彩种最小的号码,max->本彩种最大的号码 contain是不是包含对子 no 不包含  (不包含对子，默认选中的号码顺序不限，即组合方式)
+ (NSUInteger)sumForTwo:(NSArray *)array min:(int)min max:(int)max containDuiZiHao:(BOOL)contain{
    NSUInteger number = 0;
    for (int i = 0 ; i < array.count; i ++) {
        NSArray *subArr = [[[array[i]allValues] firstObject] allObjects];
        for (NSString *string in subArr) {
            int num = [string intValue];
            for (int j = min; j <= max; j ++) {
                if (num - j >= min && num - j <= max){
                    if (contain == YES )//包含对子号
                        number++;
                    else if (num - j != j && contain == NO)//不包含对子号
                        number++;
                }
            }
        }
    }
    return (contain == YES)?number:number / 2;
}
/**三位数的和值玩法,min->本彩种最小的号码,max->本彩种最大的号码
 * contain == 1 包含对子，豹子
 * contain == 2 包含对子，不包含豹子
 * contain == 3 不包含对子，豹子
 */
+ (NSUInteger)sumForThree:(NSArray *)array min:(int)min max:(int)max contain:(int)contain{
    NSUInteger number = 0;
    for (int i = 0 ; i < array.count; i ++) {
        NSArray *subArr = [[[array[i]allValues] firstObject] allObjects];
        for (NSString *string in subArr) {
            int num = [string intValue];
            for (int j = min; j <= max; j ++) {
                for (int n = min; n <= max; n++) {
                    if (num - j - n >= min && num - j - n <= max){
                        if (contain == 1 )//包含对子，豹子
                            number++;
                        else if (j != n && num - j - n != j && (num - j - n == n || num - j - n == j) && contain == 2)//包含对子，不包含豹子
                            number++;
                        else if (j != n && num - j - n != j && num - j - n != n && contain == 3){//不包含对子，豹子
                            number++;
                            DeLog(@"num : %d j : %d n : %d num-j-n : %d",num,j,n,num-j-n);
                        }
                    }
                }
            }
        }
    }
    return (contain != 3)?number:number/6;
}
///======================================================将数组转换成字符串，请求投注=====================================================//
/**
 * 将数组转换成字符串，请求投注
 */
+ (NSString *)stringWithArray:(NSArray *)array playedIdentifier:(NSString *)identifier{
    if (!array)
        return nil;
    if ([@[@"ssc_z2_r2",@"ssc_z3_r3",@"ssc_z3_r6",@"ssc_5x_r3"] containsObject:identifier])
        return [self stringWithArray:array];
    else if ([@[@"k3_3tx"] containsObject:identifier])//快三-三同通选
        return @"111,222,333,444,555,666";
    else if ([@[@"k3_3ltx"] containsObject:identifier])//快三-三同通选
        return @"123,234,345,456";
    else if ([@"bjpk10_dw" isEqualToString:identifier])//pk10-定位胆选
        return [self pkShiDingWeiXuan:array];
    else if ([@"ssc_ts_h3" isEqualToString:identifier])//时时彩-三星-后三特殊号码
        array = [self housanSpecialNumber:array];
    else if ([identifier isEqualToString:@"k3_2dx"])
        return [self kuaiSanErTongDanXuan:array];
    else if ([@[@"ssc_dxds_h2",@"three_dxds_r2",@"ssc_dxds_q3",@"ssc_dxds_h3",@"ssc_dxds_q2",@"ssl_dxds_q2dxds",@"ssl_dxds_h2dxds",@"pk10_lm_dxds1"] containsObject:identifier])
        return [self daXiaoDanShuang:array identifier:identifier];
        NSString *string;
    for (NSDictionary *dict in array) {
        NSSet *set = [dict valueForKey:[[dict allKeys] firstObject]];
        NSArray *subArray = [set allObjects];
        subArray = [NSArray ascendingArray:subArray];//升序
        NSString *subStr;
        if ([@[@"ssc_z3_q3",@"ssc_h2_zhi_hz",@"ssc_q2_zhi_hz",@"ssc_qh2_zhu_hz",@"ssc_h2_zhu_hz",@"ssc_z2_f2",@"ssc_5x_q3",@"ssc_5x_z3",@"",@"ssc_5x_h3",@"s3d_hz",@"k3_hz",@"s115_q3_zu",@"s115_q2_zu",@"ssl_3x_zxhz",@"ssl_3x_z3hz",@"ssl_3x_z6hz",@"ssl_2x_q2zf",@"ssl_2x_h2zf",@"ssc_5x_z5_120",@"ssc_4x_z4_24",@"ssc_4x_z4_6",@"k3_3dx",@"k3_3x",@"k3_2fx",@"k3_2x",@"s115_rx1",@"s115_rx2",@"s115_rx3",@"s115_rx4",@"s115_rx5",@"s115_rx6",@"s115_rx7",@"s115_rx8",@"ssl_3x_z3f",@"ssl_bdw_1mbdw",@"ssc_ts_h3",@"ssc_z3_q6",@"ssc_z3_z6",@"ssc_z3_z3"] containsObject:identifier])//和值的投注号码，要用,隔开
            subStr = [subArray componentsJoinedByString:@","];
        else
            subStr = [subArray componentsJoinedByString:@""];
        if (string.length <= 0)//拼接
            string = subStr;
        else
            string = [NSString stringWithFormat:@"%@,%@",string, subStr];
    }
    return string;
    
}
+ (NSString *)pkShiDingWeiXuan:(NSArray *)array{
    NSUInteger startCount = 0;
    NSString *string = @"";
    for (NSDictionary *dict in array) {
        NSString *key = [[dict allKeys] firstObject];
        if ([key isEqualToString:@"冠军"])
            startCount = 0;
        else if ([key isEqualToString:@"亚军"])
            startCount = 1;
        else if ([key isEqualToString:@"季军"])
            startCount = 2;
        else if ([key isEqualToString:@"第四名"])
            startCount = 3;
        else if ([key isEqualToString:@"第五名"])
            startCount = 4;
        else if ([key isEqualToString:@"第六名"])
            startCount = 5;
        else if ([key isEqualToString:@"第七名"])
            startCount = 6;
        else if ([key isEqualToString:@"第八名"])
            startCount = 7;
        else if ([key isEqualToString:@"第九名"])
            startCount = 8;
        else if ([key isEqualToString:@"第十名"])
            startCount = 9;
    }
    
    for (int i = 0; i < startCount; i++) {
        string = [string stringByAppendingString:@"-,"];
    }
    
    for (NSDictionary *dict in array) {
        NSSet *value = [[dict allValues] firstObject];
        NSArray *subArray = [value allObjects];
        subArray = [NSArray ascendingArray:subArray];//升序
        string = [string stringByAppendingString:[subArray componentsJoinedByString:@""]];
        string = [string stringByAppendingString:@","];
    }
    
    string = [string substringToIndex:string.length - 1];
    for (int i = (int)(array.count + startCount); i < 10; i++) {
        string = [string stringByAppendingString:@",-"];
    }
    
    return string;
}

//时时彩-三星-后三特殊号码
+ (NSArray *)housanSpecialNumber:(NSArray *)array{
    
    NSMutableArray *mutableArr = [NSMutableArray arrayWithArray:array];
    
    for (NSDictionary *dict in mutableArr.reverseObjectEnumerator) {
        NSMutableDictionary *mutableDic = [NSMutableDictionary dictionaryWithObject:[[dict allValues] firstObject] forKey:[[dict allKeys] firstObject]];
        
        for (NSString *number in [[[dict allValues] firstObject] allObjects]) {
            NSString *newNum = [NSString stringWithFormat:@"%@",number];
            if ([newNum isEqualToString:@"0"])
                [mutableDic setValue:[NSSet setWithObject:@"豹子"] forKey:[[dict allKeys] firstObject]];
            else if ([newNum isEqualToString:@"1"])
                [mutableDic setValue:[NSSet setWithObject:@"顺子"] forKey:[[dict allKeys] firstObject]];
            else if ([newNum isEqualToString:@"2"])
                [mutableDic setValue:[NSSet setWithObject:@"对子"] forKey:[[dict allKeys] firstObject]];
        }
        [mutableArr removeObject:dict];
        [mutableArr addObject:mutableDic];
        
    }
    return mutableArr;
    
}
//快三-二同单选
+ (NSString *)kuaiSanErTongDanXuan:(NSArray*)array{
    NSAssert(array.count == 2, @"快三二同单选的数组的元素个数不等于2");
    NSString *key1 = [[array[0] allKeys] firstObject];
    NSArray *value1 = [[[array[0] allValues] firstObject] allObjects];
    NSArray *value2 = [[[array[1] allValues] firstObject] allObjects];
    NSString *string = @"";
    for (NSString *numStr1 in value1) {
        for (NSString *numStr2 in value2) {
            if ([key1 isEqualToString:@"不同号"])
                string = [string stringByAppendingString:[NSString stringWithFormat:@"%@%@,",numStr1,numStr2]];
            else
                string = [string stringByAppendingString:[NSString stringWithFormat:@"%@%@,",numStr2,numStr1]];
        }
    }
    return [string substringToIndex:string.length - 1];
}

//大小单双
+ (NSString *)daXiaoDanShuang:(NSArray *)array identifier:(NSString *)identifier{
    NSMutableArray *mutableArr = [NSMutableArray array];
    NSString *totalStr = @"";
    for (NSDictionary *dict in array) {
        [mutableArr addObject:[[dict allKeys] firstObject]];
        NSString *string = @"";
        
        for (NSString *number in [[[dict allValues] firstObject] allObjects]) {
            if ([@[@"0",@"大"] containsObject:number])
                string = (string.length < 1)?@"大":([identifier isEqualToString:@"pk10_lm_dxds1"])?[string stringByAppendingString:@",大"]:[string stringByAppendingString:@"大"];
            else if ([@[@"1",@"小"] containsObject:number])
                string = (string.length < 1)?@"小":([identifier isEqualToString:@"pk10_lm_dxds1"])?[string stringByAppendingString:@",小"]:[string stringByAppendingString:@"小"];
            else if ([@[@"2",@"单"] containsObject:number])
                string = (string.length < 1)?@"单":([identifier isEqualToString:@"pk10_lm_dxds1"])?[string stringByAppendingString:@",单"]:[string stringByAppendingString:@"单"];
            else if ([@[@"3",@"双"] containsObject:number])
                string = (string.length < 1)?@"双":([identifier isEqualToString:@"pk10_lm_dxds1"])?[string stringByAppendingString:@",双"]:[string stringByAppendingString:@"双"];
        }
        totalStr = (totalStr.length < 1)?string:[totalStr stringByAppendingString:[NSString stringWithFormat:@",%@",string]];
    }
    if ([@[@"three_dxds_r2"] containsObject:identifier]){
        if (![mutableArr containsObject:@"百位"])
            totalStr = [@"," stringByAppendingString:totalStr];
        else if (![mutableArr containsObject:@"个位"])
            totalStr = [totalStr stringByAppendingString:@","];
        else if (![mutableArr containsObject:@"十位"]){
            NSUInteger length = [[totalStr componentsSeparatedByString:@","][0] length];
            totalStr = [[totalStr substringToIndex:length+1] stringByAppendingString:[NSString stringWithFormat:@",%@",[totalStr substringFromIndex:length+1]]];
        }
        return totalStr;
    }
    
    return totalStr;
}
//特殊的玩法，数组转换成字符串，请求投注，比如时时彩-二星组选-任二组选复式
+ (NSString *)stringWithArray:(NSArray *)array{
    NSSet *valueSet = [[array[0] allValues] firstObject];
    NSArray *newArr = [NSArray ascendingArray:[valueSet allObjects]];//升序
    NSString *valueString = [newArr componentsJoinedByString:@""];//投注号码字符串
    
    //
    NSMutableArray *mutableArr = [NSMutableArray array];
    for (NSDictionary *dict in array) {
        [mutableArr addObject:[[dict allKeys] firstObject]];
    }
    NSArray *arr = [NSArray ascendingArray:mutableArr];
    
    //
    NSString *weijiString = @"";
    for (NSString *string in arr) {
        if ([string isEqualToString:@"0"])
            weijiString = [weijiString stringByAppendingString:@"万"];
        else if ([string isEqualToString:@"1"])
            weijiString = [weijiString stringByAppendingString:@"千"];
        else if ([string isEqualToString:@"2"])
            weijiString = [weijiString stringByAppendingString:@"百"];
        else if ([string isEqualToString:@"3"])
            weijiString = [weijiString stringByAppendingString:@"十"];
        else if ([string isEqualToString:@"4"])
            weijiString = [weijiString stringByAppendingString:@"个"];
    }
    
    return [NSString stringWithFormat:@"%@,%@",weijiString, valueString];
}
@end
